﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using WindowsGame.Enums;

namespace WindowsGame.Input
{
	public class WorkInputFactory : IInputFactory
	{
		private KeyboardState currKeyboardState;
		private KeyboardState prevKeyboardState;

		public void Update(GameTime gameTime)
		{
			prevKeyboardState = currKeyboardState;
			currKeyboardState = Keyboard.GetState();
		}

		public Boolean Escape()
		{
			return KeyPress(Keys.Escape);
		}
		public Boolean Load()
		{
			return KeyHold(Keys.Enter);
		}
		public Direction Rotate()
		{
			if (KeyPress(Keys.Left))	{ return Direction.Left; }
			if (KeyPress(Keys.Right))	{ return Direction.Right; }

			return Direction.None;
		}
		public Boolean Thrust()
		{
			return KeyPress(Keys.Up);
		}
		public Boolean Brake()
		{
			return KeyPress(Keys.Down);
		}
		public Boolean Shoot()
		{
			return KeyHold(Keys.Space);
		}
		public Boolean Next()
		{
			return KeyHold(Keys.A);
		}
		public Boolean Prev()
		{
			return KeyHold(Keys.Z);
		}

		private Boolean KeyPress(Keys key)
		{
			return currKeyboardState.IsKeyDown(key);
		}
		private Boolean KeyHold(Keys key)
		{
			return currKeyboardState.IsKeyDown(key) && prevKeyboardState.IsKeyUp(key);
		}
	}
}